//
//  HAMEVC.m
//  HA
//
//  Created by sy on 2023/12/17.
//

#import "HAMEVC.h"
#import "HAUserInfoModel.h"
#import "HGJMyIncomeModel.h"
#import "HAAccountVC.h"
#import "HAOrderVC.h"
#import "HACashVC.h"
#import "HACashBackRuleVC.h"
#import "HAWebVC.h"
#import "HANavViewController.h"
#import "HALoginVC.h"
#import "AppDelegate.h"
#import "HACancelAccountPopView.h"
#import "HAYHForgetPwdVC.h"
#import "HARealNameVC.h"
#import "HAAddressListVC.h"

@interface HAMEVC ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic ,strong) NSMutableArray *dataArray;
/***/
@property (nonatomic, strong) HGJMyIncomeModel *model;
/***/
@property (nonatomic, strong) HAUserInfoModel *userInfoModel;


@end

@implementation HAMEVC

-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    [self initData];
    [self loadInfo];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initUI];
    
    [self.dataArray removeAllObjects];
    [self.dataArray addObject:@"mod1"];
    [self.dataArray addObject:@"mod2"];
    [self.dataArray addObject:@"mod3"];

    [self.tableView reloadData];
    
    // Do any additional setup after loading the view.
}

- (void)initData {
    
    SYRequest *request = [[SYRequest alloc]init];
    request.requestMethod = SYRequestMethodGET;
    request.requestUrl = @"index/income";
    
    [[SYNetworking shareSingle] hgj_asynRequestWithArray:@[request] responseBlock:^(SYDataModel *dataModel) {
        if (dataModel.request == request) {
            if (dataModel.code == 1) {
                self.model = [HGJMyIncomeModel mj_objectWithKeyValues:dataModel.data];
                [self.tableView reloadData];
            }
        }
    }];
}

-(void)loadInfo {
    SYRequest *request = [[SYRequest alloc]init];
    request.requestMethod = SYRequestMethodGET;
    request.requestUrl = @"account/info";
    
    [[SYNetworking shareSingle] hgj_asynRequestWithArray:@[request] responseBlock:^(SYDataModel *dataModel) {
        if (dataModel.request == request) {
            if (dataModel.code == 1) {
                self.userInfoModel = [HAUserInfoModel mj_objectWithKeyValues:dataModel.data];
                [self.tableView reloadData];
            }
        }
    }];
}


- (void)initUI{
    self.baseNavView.hidden = YES;
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
        make.bottom.mas_equalTo(-app_bottomH-49);
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *title = self.dataArray[indexPath.row];
    if ([title isEqualToString:@"mod1"]) {
        if (HAUserDefaults.isLock == 1) {
            return app_topH+320-75;
        }
        return app_topH+320;

    }else if ([title isEqualToString:@"mod2"]) {
        return 5+58*6;
    }else if ([title isEqualToString:@"mod3"]) {
        return 54+54+60;
    }
    return 0;
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *title = self.dataArray[indexPath.row];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"YHMeViewControllerID"];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:0 reuseIdentifier:@"YHMeViewControllerID"];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if ([title isEqualToString:@"mod1"]) {
        [self getCell3:cell.contentView];
    }else if ([title isEqualToString:@"mod2"]) {
        [self getCell4:cell.contentView];
    }else if ([title isEqualToString:@"mod3"]) {
        [self setCellOutLogin:cell.contentView];
    }
    return cell;
}

//@[@"授权证书",@"收款账户",@"收货地址",@"实名认证",@"修改登录密码",@"版本号"]
-(void)item2Click:(UITapGestureRecognizer *)tap {
    NSInteger idx = tap.view.tag;
    
    if (idx == 0) {
        HAWebVC *vc = [[HAWebVC alloc]init];
        vc.tit = @"授权证书";
        vc.urlStr = [NSString stringWithFormat:@"%@%@&token=%@",kHaBaseUrl,@"index/article/auth.html?sourde=1&token=",HAUserDefaults.token];
        [self.navigationController pushViewController:vc animated:YES];
    }else if (idx == 1) {
        HAAccountVC *vc  =[[HAAccountVC alloc]init];
        [self.navigationController pushViewController:vc animated:YES];
    }else if (idx == 2) {
        HAAddressListVC *vc = [[HAAddressListVC alloc]init];
        [self.navigationController pushViewController:vc animated:YES];
    }else if (idx == 3) {
        HARealNameVC *vc = [[HARealNameVC alloc]init];
        [self.navigationController pushViewController:vc animated:YES];
    }else if (idx == 4) {
        HAYHForgetPwdVC *vc = [[HAYHForgetPwdVC alloc]init];
        [self.navigationController pushViewController:vc animated:YES];
    }

}

-(void)cashBtnClick {
    HACashVC *vc = [[HACashVC alloc]init];
    [self.navigationController pushViewController:vc animated:YES];
}


-(void)buttonItemClick:(UIButton *)button {
    NSInteger idx = button.tag;
    
    if (idx == 0) {
        HAOrderVC *vc = [[HAOrderVC alloc]init];
        [self.navigationController pushViewController:vc animated:YES];
    }else if (idx == 1) {
        HAWebVC *vc = [[HAWebVC alloc]init];
        vc.tit = @"兑换订单";
        vc.urlStr = [NSString stringWithFormat:@"%@index/store/orders?token=%@",kHaBaseUrl,HAUserDefaults.token];
        [self.navigationController pushViewController:vc animated:YES];
    }else if (idx == 2) {
        HAWebVC *vc = [[HAWebVC alloc]init];
        vc.tit = @"在线客服";
        vc.urlStr = [NSString stringWithFormat:@"%@%@",kHaBaseUrl,@"index/article/detail?id=7"];
        [self.navigationController pushViewController:vc animated:YES];
    }

}

-(void)outButtonClick {
    [HAHelper outUserInfo];
    [AppDelegate single].window.rootViewController = [[HANavViewController alloc]initWithRootViewController:[HALoginVC new]];
}

-(void)dButtonClick {
    WS(weakSelf)
    HACancelAccountPopView *menu = [[HACancelAccountPopView alloc]init];
    [menu show];
    
    menu.HACancelAccountPopViewSureBlock = ^(NSString * _Nonnull pwd) {
        
        SYRequest *request = [[SYRequest alloc]init];
        request.requestMethod = SYRequestMethodGET;
        request.requestUrl = @"auth/cancel";
        request.parms = @{
            @"password":pwd,
        };
        [MBProgressHUD showLoading:weakSelf.view];
        
        [[SYNetworking shareSingle] hgj_asynRequestWithArray:@[request] responseBlock:^(SYDataModel *dataModel) {
            if (dataModel.request == request) {
                [MBProgressHUD hideHUDForView:weakSelf.view];
                if (dataModel.err) {
                    [MBProgressHUD showError:@"网络错误"];
                    return;
                }
                if (dataModel.code == 1) {
                    [MBProgressHUD showSuccess:@"您的账号已注销"];
                    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                        [AppDelegate single].window.rootViewController = [[HANavViewController alloc]initWithRootViewController:[HALoginVC new]];
                    });

                }else {
                    [MBProgressHUD showError:dataModel.msg];
                }
            }
        }];

    };
}



- (UITableView *)tableView {
    if (_tableView == nil) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero];
        // 去掉滚动条
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.showsHorizontalScrollIndicator = NO;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        // 设置背景颜色
        _tableView.backgroundColor = [UIColor colorWithHexString:@"#F1F3F5"];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [self.view addSubview:_tableView];
    }
    return _tableView;
}

- (NSMutableArray *)dataArray
{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array ];
    }
    return _dataArray;
}


- (void)getCell3:(UIView *)contentView {
    
    [contentView removeAllSubviews];
    contentView.backgroundColor = [UIColor colorWithHexString:@"#F1F3F5"];
    
    UIView *mainView = [UIView ha_createView];
//    mainView.backgroundColor = [UIColor whiteColor];
    mainView.frame = CGRectMake(0, 0, kScreenWidth, app_topH+360);
    [mainView makeRoundingCorners:UIRectCornerBottomLeft|UIRectCornerBottomRight cornerRadii:10];
    [contentView addSubview:mainView];
    
    UIImageView *topImageView = [UIImageView ha_createImageView];
    topImageView.userInteractionEnabled = YES;
    topImageView.image = [UIImage imageNamed:@"mebg"];
    [contentView addSubview:topImageView];
    [topImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
        make.height.mas_offset(app_topH+360);
    }];
    
    UIImageView *_iconImgView = [UIImageView ha_createImageView];
    [_iconImgView sd_setImageWithURL:[NSURL URLWithString:HAUserDefaults.avatar]];
    _iconImgView.backgroundColor = [UIColor khaBGColor];
    _iconImgView.layer.cornerRadius = 28;
    _iconImgView.layer.masksToBounds = YES;
    [topImageView addSubview:_iconImgView];
    [_iconImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-25);
        make.top.mas_equalTo(app_topH+28);
        make.width.mas_offset(56);
        make.height.mas_offset(56);
    }];
    
    UILabel *_nameLabel = [UILabel ha_NLabelWithFont:20 textColor:@"#282B2D" text:@"--" alignment:0];
    [topImageView addSubview:_nameLabel];
    [_nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(25);
        make.centerY.mas_equalTo(_iconImgView).offset(-15);
        make.right.mas_equalTo(-20);
    }];
    
    UILabel *_NoLabel = [UILabel ha_NLabelWithFont:17 textColor:@"#535B60" text:@"No.--" alignment:0];
    [topImageView addSubview:_NoLabel];
    [_NoLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_nameLabel);
        make.centerY.mas_equalTo(_iconImgView).offset(15);
        make.right.mas_equalTo(-20);
    }];
    
    CGFloat topH = app_topH+120;
    
    if (HAUserDefaults.isLock != 1) {
        
        UIView *mView = [UIView ha_createView];
        mView.backgroundColor = [UIColor colorWithHexString:@"#34A0FF"];
        mView.layer.cornerRadius = 8;
        mView.layer.masksToBounds = YES;
        mView.frame = CGRectMake(15, topH, kScreenWidth-30, 75);
        [contentView addSubview:mView];
        [mView.superview bringSubviewToFront:mView];

        FL_Button *_cashBtn = [[FL_Button alloc]initWithAlignmentStatus:FLAlignmentStatusCenter];
        [_cashBtn setTitle:@"去提现" forState:UIControlStateNormal];
        [_cashBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _cashBtn.titleLabel.font = [UIFont systemFontOfSize:15];
        _cashBtn.fl_padding = 6;
        [_cashBtn setImage:[UIImage imageNamed:@"hl_more_w"] forState:UIControlStateNormal];
        _cashBtn.backgroundColor = [UIColor colorWithHexString:@"#FF8F00"];
        _cashBtn.layer.cornerRadius = 18;
        _cashBtn.layer.masksToBounds = YES;
        [_cashBtn addTarget:self action:@selector(cashBtnClick) forControlEvents:UIControlEventTouchUpInside];
        [mView addSubview:_cashBtn];
        [_cashBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.mas_equalTo(0);
            make.right.mas_equalTo(-16);
            make.width.mas_offset(102);
            make.height.mas_offset(36);
        }];

        UILabel *titlabel = [[UILabel alloc]init];
        titlabel.text = [NSString stringWithFormat:@"我的资产（元）"];
        titlabel.textColor = [UIColor colorWithHexString:@"#ffffff"];
        titlabel.font = [UIFont systemFontOfSize:16];
        titlabel.textAlignment = 0;
        [mView addSubview:titlabel];
        [titlabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.mas_equalTo(-13);
            make.left.mas_equalTo(20);
        }];

        UILabel *conlabel = [[UILabel alloc]init];
        conlabel.text = @"0";
        conlabel.textColor = [UIColor colorWithHexString:@"#ffffff"];
        conlabel.font = [UIFont boldSystemFontOfSize:20];
        conlabel.textAlignment = 0;
        [mView addSubview:conlabel];
        [conlabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.mas_equalTo(17);
            make.left.mas_equalTo(20);
        }];
        if (![NSString isEmptyString:self.model.incomeTotal]) {
            conlabel.text = self.model.incomeTotal;
        }
        
        topH += 75;
    }
    
    topH += 16;
    
    UIView *dView = [UIView ha_createView];
    dView.layer.cornerRadius = 8;
    dView.layer.masksToBounds = YES;
    dView.backgroundColor = [UIColor whiteColor];
    [contentView addSubview:dView];
    [dView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.right.mas_equalTo(-15);
        make.top.mas_equalTo(topH);
        make.height.mas_offset(86);
    }];
    NSArray *titles;
    NSArray *images;
    titles = @[@"采购订单",@"兑换订单",@"在线客服"];
    images = @[@"mecaigou",@"meduihuan",@"mekefu"];

    NSMutableArray *buttonArray = [NSMutableArray array];
    for (int i = 0; i < titles.count; i++) {
        FL_Button *button = [[FL_Button alloc]initWithAlignmentStatus:FLAlignmentStatusTop];
        [button setTitle:titles[i] forState:UIControlStateNormal];
        button.titleLabel.font = [UIFont systemFontOfSize:14];
        [button setTitleColor:[UIColor colorWithHexString:@"#222C35"] forState:UIControlStateNormal];
        [button setImage:[UIImage imageNamed:images[i]] forState:UIControlStateNormal];
        button.fl_padding = 13;
        button.tag = i;
        [button addTarget:self action:@selector(buttonItemClick:) forControlEvents:UIControlEventTouchUpInside];
        [dView addSubview:button];
        [buttonArray addObject:button];
    }
    [buttonArray mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedItemLength:62 leadSpacing:40 tailSpacing:40];
    [buttonArray mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_offset(50);
        make.top.mas_equalTo(20);
    }];
    
    
    [_iconImgView setImageWithURL:[NSURL URLWithString:HAUserDefaults.avatar] placeholder:nil];
    
    NSString *name = HAUserDefaults.name;
    if ([NSString isEmptyString:name]) {
        name = HAUserDefaults.username;
    }
    NSString *grades_name = self.userInfoModel.gradeslevelname;
    if (![NSString isEmptyString:grades_name]) {
        NSMutableAttributedString *attr = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"%@  %@",name,grades_name]];
        [attr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:14] range:[[attr string]rangeOfString:grades_name]];
        [attr addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithHexString:@"#34A0FF"] range:[[attr string]rangeOfString:grades_name]];
        _nameLabel.attributedText = attr;
    }else {
        _nameLabel.text = name;
    }
    
    NSString *userID = [NSString stringWithFormat:@"No:%@", HAUserDefaults.userID] ;
    _NoLabel.text = userID;
}


- (void)getCell4:(UIView *)contentView {
    
    [contentView removeAllSubviews];
    contentView.backgroundColor = [UIColor colorWithHexString:@"#F1F3F5"];
    
    UIView *mainView = [UIView ha_createView];
    mainView.backgroundColor = [UIColor whiteColor];
    if (HAUserDefaults.isLock == 1) {
        mainView.frame = CGRectMake(15, 5, kScreenWidth-30, 58*5);
    }else {
        mainView.frame = CGRectMake(15, 5, kScreenWidth-30, 58*6);
    }
    [mainView makeRoundingCorners:UIRectCornerTopLeft|UIRectCornerTopRight|UIRectCornerBottomLeft|UIRectCornerBottomRight cornerRadii:10];
    [contentView addSubview:mainView];
    
    NSArray *titles = @[@"授权证书",@"收款账户",@"收货地址",@"实名认证",@"修改登录密码",@"版本号"];

    for (int i = 0; i < titles.count; i ++) {
        NSString *title = titles[i];
        UIView *view = [UIView ha_createView];
        view.tag = i;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(item2Click:)];
        [view addGestureRecognizer:tap];
        [mainView addSubview:view];
        [view mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(i*58);
            make.left.mas_equalTo(0);
            make.right.mas_equalTo(0);
            make.height.mas_offset(58);
        }];
                
        UILabel *titleLabel = [[UILabel alloc]init];
        titleLabel.text = titles[i];
        titleLabel.textColor = [UIColor colorWithHexString:@"#222C35"];
        titleLabel.font = [UIFont systemFontOfSize:16];
        [view addSubview:titleLabel];
        [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.mas_equalTo(view);
            make.left.mas_equalTo(20);
            make.width.mas_offset(@108);
        }];
        
        UIImageView *rImgV = [UIImageView ha_createImageView];
        rImgV.image = [UIImage imageNamed:@"ic_cell_more"];
        [view addSubview:rImgV];
        [rImgV mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.mas_offset(5);
            make.height.mas_offset(10);
            make.centerY.mas_equalTo(0);
            make.right.mas_equalTo(-20);
        }];
        
        UILabel *contentLabel = [[UILabel alloc]init];
        contentLabel.textColor = [UIColor colorWithHexString:@"#666666"];
        contentLabel.font = [UIFont systemFontOfSize:14];
        contentLabel.textAlignment = NSTextAlignmentRight;
        [view addSubview:contentLabel];
        [contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(view);
            make.left.equalTo(titleLabel).offset(15);
            make.right.equalTo(rImgV.mas_left).offset(-5);
        }];
        
        
        UIView *lineView = [UIView ha_createView];
        lineView.backgroundColor = [UIColor khaBGColor];
        [view addSubview:lineView];
        [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(view).offset(15);
            make.right.equalTo(view).offset(-15);
            make.height.equalTo(@1);
            make.bottom.equalTo(view);
        }];
        
        
        if (i == titles.count-1) {
            lineView.hidden = YES;
        }
        
        if ([title isEqualToString:@"版本号"]) {
            contentLabel.text = @"v1.0.0";
            rImgV.hidden = YES;
        }
        
    }
    
}

-(void)setCellOutLogin:(UIView *)contentView {
    for (UIView *view in contentView.subviews) {
        [view removeFromSuperview];
    }
    
    contentView.backgroundColor = [UIColor khaBGColor];
    
    UIButton *outButton = [[UIButton alloc]init];
    [outButton setTitle:@"退 出" forState:UIControlStateNormal];
    [outButton setTitleColor:[UIColor colorWithHexString:@"#676ADD"] forState:UIControlStateNormal];
    outButton.titleLabel.font = [UIFont boldSystemFontOfSize:20];
    outButton.layer.cornerRadius = 4;
    outButton.layer.masksToBounds = YES;
    outButton.layer.borderColor = [UIColor colorWithHexString:@"#676ADD"].CGColor;
    outButton.layer.borderWidth = 0.6;
    [outButton addTarget:self action:@selector(outButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [contentView addSubview:outButton];
    [outButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(25);
        make.right.mas_equalTo(-25);
        make.centerY.mas_equalTo(-40);
        make.height.mas_offset(54);
    }];
    
    UIButton *dButton = [[UIButton alloc]init];
    [dButton setTitle:@"注销账户 >>>" forState:UIControlStateNormal];
    [dButton setTitleColor:[UIColor colorWithHexString:@"#666666"] forState:UIControlStateNormal];
    dButton.titleLabel.font = [UIFont systemFontOfSize:12];
    [dButton addTarget:self action:@selector(dButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [contentView addSubview:dButton];
    [dButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(30);
        make.centerX.mas_equalTo(0);
        make.height.mas_offset(20);
        make.width.mas_offset(88);
    }];

}

@end
